﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace SasPlugIn
{
    public class DataLinkParameters
    {
        #region Properties

        public string FormatLibraryPath { get; set; }
        public string DataEntryLibraryPath { get; set; }
        public string DataOutputLibraryPath { get; set; }
        public string DataSetEntryName { get; set; }
        public string DataSetOutputName { get; set; }

        public bool IsFilled
        {
            get
            {
                return 
                    string.IsNullOrEmpty(FormatLibraryPath) == false &&
                    string.IsNullOrEmpty(DataEntryLibraryPath) == false &&
                    string.IsNullOrEmpty(DataOutputLibraryPath) == false &&
                    string.IsNullOrEmpty(DataSetEntryName) == false &&
                    string.IsNullOrEmpty(DataSetOutputName) == false;
            }
        }
        #endregion

        #region Serialization

        /// <summary>
        /// Update instance of this class according to XML doc.
        /// </summary>
        /// <param name="reader"> a text reader instance. </param>
        public void XmlRead(string filepath)
        {
            TextReader reader = null;
            DataLinkParameters dlp = null;
            try
            {
                reader = new StreamReader(filepath);

                XmlSerializer xmlSer = new XmlSerializer(this.GetType());
                dlp = xmlSer.Deserialize(reader) as DataLinkParameters;

                // now update ourself
                this.FormatLibraryPath = dlp.FormatLibraryPath;
                this.DataEntryLibraryPath = dlp.DataEntryLibraryPath;
                this.DataOutputLibraryPath = dlp.DataOutputLibraryPath;
                this.DataSetEntryName = dlp.DataSetEntryName;
                this.DataSetOutputName = dlp.DataSetOutputName;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to read. Invalid XML document", ex);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        /// <summary>
        /// Xml serialization in specified filename
        /// </summary>
        /// <param name="xmlFile"> filename to create</param>
        public virtual void XmlWrite(string xmlFile)
        {
            StreamWriter sw = new StreamWriter(xmlFile);
            XmlWrite(sw);
        }

        /// <summary> XML Persistence using writer. </summary>
        /// <param name="tw"> an object derived from TextWriter</param>
        public virtual void XmlWrite(TextWriter tw)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(this.GetType());
                xmlSer.Serialize(tw, this);
            }
            finally
            {
                tw.Close();
            }
        }
        #endregion

    }
}
